package com.intranet.fund.util;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.water.listener.Config;

/**
 * 邮件操作类
 * 
 * @author Administrator
 * 
 */
public class EmailUtil {

	private String to = ""; // 收件人地址
	private String subject = "新用户注册"; // 邮件标题

	public void setAddress(String to) {
		this.to = to;
		this.subject = subject;
	}

	public void send(String txt) {
		Properties p = Config.getProps();
		Properties props = new Properties();
		props.put("mail.smtp.host", p.getProperty("host"));
		props.put("mail.smtp.auth", "true");
		Session session = Session.getDefaultInstance(props);
		session.setDebug(false);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(p.getProperty("sendFrom"),
					"公积金网"));
			String[] toFrom = to.split(";");
			InternetAddress[] toAddress = new InternetAddress[toFrom.length];
			for (int i = 0; i < toFrom.length; i++) {
				toAddress[i] = new InternetAddress(toFrom[i]);
			}
			message.addRecipients(Message.RecipientType.TO, toAddress);
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setText(txt);
			multipart.addBodyPart(contentPart);
			message.setContent(multipart);
			message.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect(p.getProperty("host"), p.getProperty("username"),
					p.getProperty("password"));
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
